import os
import shutil
import time
from Utils import *
file_path = os.path.abspath(__file__)
dir_path = os.path.dirname(file_path).replace("\\file","")
srcAssets = dir_path.replace("tools","")+'build\\jsb-default\\publish\\android'
print("apk路径::\n"+srcAssets)

with open(dir_path.replace("\\tools","\\assets\\resources\\tvBoxDatas\\androidBroadcast.json"), 'r') as file:
    jsonBroadcastData = json.load(file)


desAssets = dir_path.replace("\\tools","\\settings\\builder.json")
with open(desAssets, 'r') as file:
    jsonData = json.load(file)
with open(dir_path +'\\file\\values.json', 'r', encoding='utf-8') as files:
    values = json.load(files)
if jsonData['android']['packageName'].find("jufeng") > 0 :
    appName = values["jufeng"]
elif jsonData['android']['packageName'].find("chensheng") > 0 :
    appName = values["chensheng"]
appVersionName = values["versionName"]
if(os.path.exists(srcAssets)):
    copyFiles(srcAssets, dir_path)
    apk_path = get_files_list(dir_path)[0] 
    apk_name = os.path.basename(apk_path)
    exit_name = ""
    if(jsonBroadcastData['isOnceExitGame']):
        exit_name = "fh"
    if(jsonBroadcastData['isBroadcast']):
        apk_newname = time.strftime(appName+'_v'+appVersionName +'_%Y%m%d_ydgb'+exit_name+'.apk',time.localtime(time.time()))
    else:
        apk_newname = time.strftime(appName+'_v'+appVersionName +'_%Y%m%d'+exit_name+'.apk',time.localtime(time.time()))
    if(os.path.exists(apk_newname)):
        os.remove(apk_newname)
    os.rename(apk_name, apk_newname)
shutil.rmtree(dir_path+'\\file\\__pycache__')
print('完成任务')